<template>
  <div class="app-container">
    <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="污染源单位整体应急预案" prop="rpsAllPlan">
                <el-input
                  v-model="queryParams.rpsAllPlan"
                  placeholder="请输入污染源单位整体应急预案"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="分项应急预案" prop="rpsSubPlan">
                <el-input
                  v-model="queryParams.rpsSubPlan"
                  placeholder="请输入分项应急预案"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="应急保障费用" prop="rpsMoney">
                <el-input
                  v-model="queryParams.rpsMoney"
                  placeholder="请输入应急保障费用"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="X年度应急保障实际费用" prop="rpsActualMoney">
                <el-input
                  v-model="queryParams.rpsActualMoney"
                  placeholder="请输入X年度应急保障实际费用"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="日期" prop="rpsDate">
                <el-date-picker clearable size="small"
                  v-model="queryParams.rpsDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="事故级别" prop="rpsLv">
                <el-input
                  v-model="queryParams.rpsLv"
                  placeholder="请输入事故级别"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="事故报告" prop="rpsReport">
                <el-input
                  v-model="queryParams.rpsReport"
                  placeholder="请输入事故报告"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:against:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:against:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:against:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:against:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="againstList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <!--      <el-table-column align="center" label="风险防范体系ID" prop="rpsId"/>-->
      <el-table-column align="center" label="污染源单位整体应急预案" prop="rpsAllPlan">
        <template slot-scope="scope" v-if="scope.row.rpsAllPlan">
          <a :href="`${baseUrl}${scope.row.rpsAllPlan}`" target="_blank"
             class="buttonText">{{
              scope.row.rpsAllPlan.toString().substring(scope.row.rpsAllPlan.lastIndexOf("/") + 1)
            }}</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="分项应急预案" prop="rpsSubPlan">
        <template slot-scope="scope" v-if="scope.row.rpsSubPlan">
          <a :href="`${baseUrl}${scope.row.rpsSubPlan}`" target="_blank"
             class="buttonText">{{
              scope.row.rpsSubPlan.toString().substring(scope.row.rpsSubPlan.lastIndexOf("/") + 1)
            }}</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="应急保障费用" prop="rpsMoney">
        <template slot-scope="scope">
          {{ scope.row.rpsMoney }}万元
        </template>
      </el-table-column>
      <el-table-column align="center" label="突发环境事故处置日期" prop="rpsDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rpsDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="事故级别" prop="rpsLv"/>
      <el-table-column align="center" label="事故报告" prop="rpsReport">
        <template slot-scope="scope" v-if="scope.row.rpsReport">
          <a :href="`${baseUrl}${scope.row.rpsReport}`" target="_blank"
             class="buttonText">{{
              scope.row.rpsReport.toString().substring(scope.row.rpsReport.lastIndexOf("/") + 1)
            }}</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="总结材料" prop="rpsMaterial">
        <template slot-scope="scope" v-if="scope.row.rpsMaterial">
          <a :href="`${baseUrl}${scope.row.rpsMaterial}`" target="_blank"
             class="buttonText">{{
              scope.row.rpsMaterial.toString().substring(scope.row.rpsMaterial.lastIndexOf("/") + 1)
            }}</a>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="照片" prop="rpsPhoto"/>-->
      <!--      <el-table-column align="center" label="视频" prop="rpsVideo"/>-->
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['safe:against:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['safe:against:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改风险防范体系对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules" class="clearfix  m-f-a-i" label-width="180px">
        <div class="content-left" style="width: 45%">
          <div class="title2"><span> </span>应急预案</div>
          <div class="line">
            <el-form-item label="污染源单位整体应急预案" prop="rpsAllPlan">
              <FileUpload v-model="form.rpsAllPlan"/>
            </el-form-item>
          </div>
          <el-form-item label="分项应急预案" prop="rpsSubPlan">
            <FileUpload v-model="form.rpsSubPlan"/>
          </el-form-item>
          <el-form-item label="应急保障费用(万元)" prop="rpsMoney">
            <el-input v-model="form.rpsMoney" placeholder="请输入应急保障费用(万元)"/>
          </el-form-item>
          <!--          <div class="line">
                      <el-form-item label="X年度应急保障实际费用" prop="rpsActualMoney">
                        <el-input v-model="form.rpsActualMoney" placeholder="请输入X年度应急保障实际费用"/>
                      </el-form-item>
                    </div>-->


        </div>

        <div class="content-right" style="float: right; width: 45%">
          <div class="title2"><span> </span>突发环境事故的处置</div>
          <el-form-item label="日期" prop="rpsDate">
            <el-date-picker v-model="form.rpsDate" clearable
                            placeholder="选择日期"
                            size="small"
                            style="width: 100%"
                            type="date"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="事故报告" prop="rpsReport">
            <FileUpload v-model="form.rpsReport"/>
          </el-form-item>
          <el-form-item label="事故级别" prop="rpsLv">
            <el-input v-model="form.rpsLv" placeholder="请输入事故级别"/>
          </el-form-item>
          <el-form-item label="总结材料" prop="rpsMaterial">
            <FileUpload v-model="form.rpsMaterial"/>
          </el-form-item>
          <el-form-item label="照片" prop="rpsPhoto">
              <div style="height: 1000%;"><ImageUpload v-model="form.rpsPhoto"/></div>
          </el-form-item>
          <el-form-item label="视频" prop="rpsVideo">
            <VideoUpload v-model="form.rpsVideo"/>
          </el-form-item>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addAgainst, delAgainst, exportAgainst, getAgainst, listAgainst, updateAgainst} from "@/api/safe/against";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isNotMUstTwoPoint,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Against",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风险防范体系表格数据
      againstList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rpsAllPlan: null,
        rpsSubPlan: null,
        rpsMoney: null,
        rpsActualMoney: null,
        rpsDate: null,
        rpsLv: null,
        rpsReport: null,
        rpsMaterial: null,
        rpsPhoto: null,
        rpsVideo: null,
        epId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rpsAllPlan: [
          {required: true, message: "污染源单位整体应急预案不能为空", trigger: "blur"}
        ],
        rpsSubPlan: [
          {required: true, message: "分项应急预案不能为空", trigger: "blur"}
        ],
        rpsMoney: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {



    /** 查询风险防范体系列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listAgainst(this.queryParams).then(response => {
            this.againstList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        epId:null,
        rpsId: null,
        rpsAllPlan: null,
        rpsSubPlan: null,
        rpsMoney: null,
        rpsActualMoney: null,
        rpsDate: null,
        rpsLv: null,
        rpsReport: null,
        rpsMaterial: null,
        rpsPhoto: null,
        rpsVideo: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rpsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风险防范体系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rpsId = row.rpsId || this.ids
      getAgainst(rpsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改风险防范体系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rpsId != null) {
            updateAgainst(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addAgainst(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rpsIds = row.rpsId || this.ids;
      this.$confirm('是否确认删除风险防范体系编号为"' + rpsIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delAgainst(rpsIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有风险防范体系数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAgainst(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
